var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var organization = require('./routes/company');
var organizationType = require('./routes/organizationType');
var user = require('./routes/user');
var role = require('./routes/role');
var authority = require('./routes/authority');
var project = require('./routes/project');
var resident = require('./routes/resident')

var app = express();//获得 express 实例

// view engine setup
app.set('views', path.join(__dirname, 'views')); //使用views下面的jade
app.set('view engine', 'jade');

//使用全局中间件
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//使用路由组件
app.use('/organization', organization);
app.use('/organizationType', organizationType);
app.use('/user', user);
app.use('/role', role);
app.use('/authority', authority);
app.use('/project', project);

//2017/10/31 万圣节 门禁管理页面
app.use('/resident',resident)
app.use('/card',require('./routes/card'))
app.use('/terminal',require('./routes/terminal'))
app.use('/building',require('./routes/building'))
app.use('/room',require('./routes/room'))

//2017/10/24 ACS项目更换请求路径
app.get('/sysAreas/sysAreaList',function(req,res,next){
    if(req.query.id == 'S'){
        req.query.id = 0
    }
    var calculatedData = req.query.id * 2 + 1
    res.json({
        "message": "测试内容9so1",
        "result": true,
        "resultData": [
            {
                "id": calculatedData,
                "name": calculatedData
            },
            {
                "id": calculatedData + 1,
                "name": calculatedData + 1
            },
        ]
    })
})


// catch 404 and forward to error handler
// 因为上面的没匹配到，所以这是最后一个
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
